package com.asiainfo.ncrm.ant;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.asiainfo.ncrm.base.LoadImage;
import com.asiainfo.ncrm.util.SharedUtil;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class AntApplication extends Application {

	private static Context context;
	private MessageReceiver mMessageReceiver;
	public static final String MESSAGE_RECEIVED_ACTION = "com.asiainfo.ncrm.ant.MESSAGE_RECEIVED_ACTION";
	public static final String KEY_TITLE = "title";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_EXTRAS = "extras";

//	public static List<Banner> banners = new ArrayList<Banner>();

	public static Context getInstance() {
		return context;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		context = this.getApplicationContext();
		JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this); // 初始化 JPush
		JPushInterface.setLatestNotificationNumber(context, 1);
		if (SharedUtil.getBoolean("sound", false)) {
			JPushInterface.setSilenceTime(this, 23, 58, 23, 59);
		}else {
			JPushInterface.setSilenceTime(this, 00, 00, 23, 59);
		}
		
		registerMessageReceiver();
		// 百度
//		SDKInitializer.initialize(this);
		ImageLoader.getInstance().init(LoadImage.config());

	}

//	public static List<Banner> getBanner() {
//		return banners;
//	}

	public static Context getApplication() {
		return context;
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		JPushInterface.stopPush(getApplicationContext());
		unregisterReceiver(mMessageReceiver);
	}

	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
			String messge = intent.getStringExtra(KEY_MESSAGE);
			// String extras = intent.getStringExtra(KEY_EXTRAS);
			// StringBuilder showMsg = new StringBuilder();
			// showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
			// if (!ExampleUtil.isEmpty(extras)) {
			// showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
			// }
			// setCostomMsg(showMsg.toString());
			// }
		}
	}
}
